package com.gree.scmservice.controller;

import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.common.enums.BusinessType;
import com.gree.framework.entity.SysUser;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.entity.smcom.Notice;
import com.gree.scmservice.service.GscmSysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.nutz.dao.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/notice")
@Api(tags = "系统信息:通知公告")
public class SysNoticeController
{
    @Autowired
    private GscmSysNoticeService gscmSysNoticeService;

    /**
     * 获取通知公告列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典类型")
    @ApiImplicitParam(name = "notice", value = "通知公告", dataType = "Notice")
    public ReturnVo list(Notice notice)
    {
        TablePageData<Record> recordTablePageData = gscmSysNoticeService.selectNoticeList(notice);
        return ReturnVo.success(recordTablePageData);
    }

    /**
     * 获取基地通知公告列表
     */
    @GetMapping("/complist")
    @ApiOperation(value = "获取基地通知公告列表")
    @ApiImplicitParam(name = "notice", value = "通知公告", dataType = "Notice")
    public ReturnVo complist(Notice notice)
    {
        notice.setComp(SecurityUtils.getUser().getComp());
        notice.setStyp(2L);
        TablePageData<Record> recordTablePageData = gscmSysNoticeService.selectNoticeList(notice);
        return ReturnVo.success(recordTablePageData);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping(value = "/{noticeId}")
    @ApiOperation(value = "根据通知公告编号获取详细信息")
    @ApiImplicitParam(name = "noticeId", value = "通知公告ID", dataType = "Long")
    public ReturnVo getInfo(@PathVariable Long noticeId)
    {
        Notice notice = gscmSysNoticeService.selectNoticeById(noticeId);
        return ReturnVo.success(notice);
    }

    /**
     * 新增通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "分页查询字典类型")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo add(Notice notice, @RequestParam MultipartFile[] files) throws IOException {
        int beforefilesize = files.length;
        ArrayList dutalist = gscmSysNoticeService.uploadNoticeFile(notice.getComp(),files);
        if(dutalist.size() == beforefilesize){
            notice.setDuta(dutalist);
            int resultNum = gscmSysNoticeService.insertNotice(notice);
            if(resultNum > 0){
                return new ReturnVo("200","success");
            }else{
                return new ReturnVo("500","fail");
            }
        }else{
            return new ReturnVo("500","fail");
        }
    }

    /**
     * 修改通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "分页查询字典类型")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo edit(@Validated Notice notice, @RequestParam MultipartFile[] files) throws IOException {
        int beforefilesize = files.length;
        ArrayList dutalist = gscmSysNoticeService.uploadNoticeFile(notice.getComp(),files);
        if(dutalist.size() == beforefilesize){
            notice.setDuta(dutalist);
            int resultNum = gscmSysNoticeService.updateNotice(notice);
            if(resultNum > 0){
                return new ReturnVo("200","success");
            }else{
                return new ReturnVo("500","fail");
            }
        }else{
            return new ReturnVo("500","fail");
        }
    }


    /**
     * 删除通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/file")
    @ApiOperation(value = "分页查询字典类型")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo removeFile(@Validated @RequestBody Notice notice)
    {
        gscmSysNoticeService.deleteNoticeFile(notice);
        return new ReturnVo("200","success");
    }

    /**
     * 删除通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation(value = "分页查询字典类型")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo remove(@Validated @RequestBody Notice notice)
    {
        gscmSysNoticeService.deleteNotice(notice);
        return new ReturnVo("200","success");
    }
}
